package com.paiai.lslease.action.landlordauth;

import com.paiai.lslease.auto.model.AppLandlordAuth;
import com.paiai.lslease.base.BaseAction;
import com.paiai.lslease.model.AppLandlordAuthHouseInfo;
import com.paiai.lslease.service.apphouse.AppHouseService;
import com.paiai.lslease.service.appuser.AppSysUserService;
import com.paiai.lslease.service.landlordauth.AppLandlordAuthService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.URLDecoder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class AppLandlordAuthAction extends BaseAction {

    private static final Logger logger = LoggerFactory.getLogger(AppLandlordAuthAction.class);
    @Autowired
    private AppLandlordAuthService appLandlordAuthService;
    @Autowired
    private AppSysUserService appSysUserService;
    @Autowired
    private AppHouseService appHouseService;

    private String landlordId;
    private String landlordAuthId;
    private String operateUserId;
    private String houseIdArrString;
    private String userMobile;
    private String userTip;

    public void setLandlordId(String landlordId) {
        this.landlordId = landlordId;
    }

    public void setLandlordAuthId(String landlordAuthId) {
        this.landlordAuthId = landlordAuthId;
    }

    public void setOperateUserId(String operateUserId) {
        this.operateUserId = operateUserId;
    }

    public void setHouseIdArrString(String houseIdArrString) {
        this.houseIdArrString = houseIdArrString;
    }

    public void setUserMobile(String userMobile) {
        this.userMobile = userMobile;
    }

    public void setUserTip(String userTip) {
        this.userTip = userTip;
    }

    /**
     * 房东授权房源列表给用户
     */
    public void authHouseListToUser() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            if (!appSysUserService.userBelongLandlord(Integer.parseInt(operateUserId)) && !appSysUserService.userBelongLandlordAuthManagerById(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者不是房东，也不是被授权的房源管理者，不能授权房源信息");
                return;
            }
            if (StringUtils.isEmpty(userMobile)) {
                sendFailureMsg("被授权的用户的手机号码不能为空");
                return;
            }
            if (!StringUtils.isNumeric(userMobile) || userMobile.length() != 11) {
                sendFailureMsg("被授权的用户的手机号码格式错误");
                return;
            }
            if (appSysUserService.getAppSysUserInfoByMobile(userMobile) == null) {
                sendFailureMsg("被授权的用户不存在");
                return;
            }
            if (StringUtils.isEmpty(userTip)) {
                userTip = userMobile + "被授权房源信息";
            }
            if (StringUtils.isEmpty(houseIdArrString)) {
                sendFailureMsg("授权的房源信息不能为空");
                return;
            }
            List<AppLandlordAuth> appLandlordAuthList = new ArrayList<AppLandlordAuth>();
            if (houseIdArrString.contains("-")) {
                String[] houseIdArr = houseIdArrString.split("-");
                if (houseIdArr.length == 0) {
                    sendFailureMsg("授权的房源信息格式错误，无法获取房源信息");
                    return;
                }
                for (String houseId : houseIdArr) {
                    if (!checkAuthHouseForInsert(appLandlordAuthList, houseId)) {
                        return;
                    }
                }
            } else {
                if (!checkAuthHouseForInsert(appLandlordAuthList, houseIdArrString)) {
                    return;
                }
            }

            if (appLandlordAuthList.size() == 0) {
                sendSuccesseMsg("用户没有待授权的房源信息");
                return;
            }
            List<AppLandlordAuthHouseInfo> appLandlordAuthHouseInfoList = appLandlordAuthService.insertAppLandlordAuthList(appLandlordAuthList);
            if (appLandlordAuthHouseInfoList == null) {
                sendFailureMsg("房东授权房源失败");
                return;
            }
            sendSuccesseMsg(appLandlordAuthHouseInfoList, "房东授权房源列表成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("修改授权房源列表失败");
        }
    }

    /**
     * 查询房东授权的房源列表
     */
    public void queryHouseListAuthedByLandlord() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            if (StringUtils.isEmpty(landlordId)) {
                sendFailureMsg("房东ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(landlordId)) {
                sendFailureMsg("房东ID格式错误");
                return;
            }
            if (!appSysUserService.userBelongLandlord(Integer.parseInt(landlordId))) {
                sendFailureMsg("房东不存在");
                return;
            }
            sendSuccesseMsg(appLandlordAuthService.selectAppLandlordAuthListByLandlordId(landlordId), "查询房东授权给用户的房源列表信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询房东授权给用户的房源列表信息失败");
        }
    }

    /**
     * 查询房东授权的所有管理员
     */
    public void queryAllAuthedManager() {
        try {
            if (StringUtils.isEmpty(landlordId)) {
                sendFailureMsg("房东ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(landlordId)) {
                sendFailureMsg("房东ID格式错误");
                return;
            }
            if (!appSysUserService.userBelongLandlord(Integer.parseInt(landlordId))) {
                sendFailureMsg("房东不存在");
                return;
            }
            sendSuccesseMsg(appLandlordAuthService.queryAllAuthedManager(Integer.parseInt(landlordId)), "查询房东授权的所有管理员成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询房东授权的所有管理员失败");
        }
    }

    /**
     * 查询授权管理员管理的所有房源列表
     */
    public void queryHouseListAuthedByManagerMobile() {
        try {
            if (StringUtils.isEmpty(userMobile)) {
                sendFailureMsg("授权管理员手机号码不能为空");
                return;
            }
            if (!StringUtils.isNumeric(userMobile) || userMobile.length() != 11) {
                sendFailureMsg("授权管理员的手机号码格式错误");
                return;
            }
            if (appSysUserService.getAppSysUserInfoByMobile(userMobile) == null) {
                sendFailureMsg("授权管理员不存在");
                return;
            }
            sendSuccesseMsg(appLandlordAuthService.selectAppLandlordAuthListByUserMobile(userMobile), "查询授权管理员管理的所有房源列表成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("查询授权管理员管理的所有房源列表失败");
        }
    }

    /**
     * 房东删除授权给用户的一个房源信息
     */
    public void deleteHouseAuthed() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            if (!appSysUserService.userBelongLandlord(Integer.parseInt(operateUserId)) && !appSysUserService.userBelongLandlordAuthManagerById(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者不是房东，也不是被授权的房源管理者，不能删除授权的房源信息");
                return;
            }
            if (StringUtils.isEmpty(landlordAuthId)) {
                sendFailureMsg("授权ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(landlordAuthId)) {
                sendFailureMsg("授权ID格式错误");
                return;
            }
            if (appLandlordAuthService.selectAppLandlordAuth(Integer.parseInt(landlordAuthId)) == null) {
                sendFailureMsg("要删除授权信息不存在");
                return;
            }
            if (!appLandlordAuthService.deleteAppLandlordAuth(Integer.parseInt(landlordAuthId))) {
                sendFailureMsg("删除授权信息失败");
                return;
            }
            sendSuccesseMsg("删除授权信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("删除授权信息失败");
        }
    }

    /**
     * 房东删除一个授权管理员
     */
    public void deleteAuthedManager() {
        try {
            if (StringUtils.isEmpty(operateUserId)) {
                sendFailureMsg("操作者ID不能为空");
                return;
            }
            if (!StringUtils.isNumeric(operateUserId)) {
                sendFailureMsg("操作者ID格式错误");
                return;
            }
            if (!appSysUserService.checkUserRegisterState(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者未注册");
                return;
            }
            if (!appSysUserService.userBelongLandlord(Integer.parseInt(operateUserId))) {
                sendFailureMsg("操作者不是房东，不能删除授权的管理员");
                return;
            }
            if (StringUtils.isEmpty(userMobile)) {
                sendFailureMsg("授权管理员手机号码不能为空");
                return;
            }
            if (!StringUtils.isNumeric(userMobile) || userMobile.length() != 11) {
                sendFailureMsg("授权管理员手机号码格式错误");
                return;
            }
            if (!appLandlordAuthService.deleteAuthedManager(userMobile)) {
                sendFailureMsg("房东删除一个授权管理员失败");
                return;
            }
            sendSuccesseMsg("房东删除一个授权管理员成功");
        } catch (Exception e) {
            e.printStackTrace();
            sendFailureMsg("房东删除一个授权管理员失败");
        }
    }

    private boolean checkAuthHouseForInsert(List<AppLandlordAuth> appLandlordAuthList, String houseId) throws Exception {
        if (StringUtils.isEmpty(houseId)) {
            sendFailureMsg("房源ID不能为空");
            return false;
        }
        if (!StringUtils.isNumeric(houseId)) {
            sendFailureMsg("房源ID格式错误,授权的房源信息必须以-分隔，如10010-10011-10012");
            return false;
        }
        if (!appHouseService.checkHouseExist(Integer.parseInt(houseId))) {
            sendFailureMsg("房源" + houseId + "不存在");
            return false;
        }
        if (!appHouseService.houseBelongUser(Integer.parseInt(houseId), Integer.parseInt(operateUserId))) {
            sendFailureMsg("被授权的房源" + houseId + "不属于用户" + operateUserId);
            return false;
        }
        boolean hasHouse = appLandlordAuthService.userHasAuthedHouse(userMobile, Integer.parseInt(houseId));
        if (hasHouse) {
            logger.info("用户" + userMobile + "已有被授权的房源" + houseId + "，不重复授权");
        }
        if (!hasHouse) {//只授权用户没有的房源
            AppLandlordAuth appLandlordAuth = new AppLandlordAuth();
            appLandlordAuth.setLandlordId(Integer.valueOf(operateUserId));
            appLandlordAuth.setHuoseId(Integer.valueOf(houseId));
            appLandlordAuth.setUserMobile(userMobile);
            appLandlordAuth.setUserTip(userTip);
            appLandlordAuth.setItem1("1");
            appLandlordAuth.setInsertTime(new Date());
            appLandlordAuth.setUpdateTime(new Date());
            appLandlordAuthList.add(appLandlordAuth);
        }
        return true;
    }
}
